<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<?php
		
	include('./index/header.html');	
		
	// connect to the database
	include('Connect_DB.php');
	include('stdlib.php');
	include('Model.php');
	
	// number of results to show per page
	$per_page = 3;
	
	// figure out the total pages in the database
	$result = View_Donvi();
	
	//Count rows within table "Nganh"
	$total_results = mysql_num_rows($result);
	$total_pages = ceil($total_results / $per_page);

	// check if the 'page' variable is set in the URL (ex: Paginated_Nganh.php?page=1)
	if (isset($_GET['page']) && is_numeric($_GET['page']))
	{
			$show_page = $_GET['page'];
			
			// make sure the $show_page value is valid
			if ($show_page > 0 && $show_page <= $total_pages)
			{
					$start = ($show_page -1) * $per_page;
					$end = $start + $per_page; 
			}
			else
			{
					// error - show first set of results
					$start = 0;
					$end = $per_page; 
			}               
	}
	else
	{
			// if page isn't set, show first set of results
			$start = 0;
			$end = $per_page; 
	}
	
	echo "</p>";
	
	
	echo form_open("Quan ly don vi dao tao");
	echo '<h2><center> QUẢN LÝ ĐƠN VỊ ĐÀO TẠO </center></h2><br /><hr><br />';
	
	//Render link View_View_Nganh.php
	echo "<center><p><a href='View_View_Donvi.php'><b>View All Pages</b></a></center>";
	
	// display pagination
	for ($i = 1; $i <= $total_pages; $i++)
	{
			echo "<a href='Paginated_Donvi.php?page=$i'>$i</a> ";
	}
	
	//View Form
	echo tbl_open('100%', '2');
	echo tr(td('STT', '15%').td('Mã Đơn Vị', '15%').td('Tên Khoa ĐT', '25%').td('Tên Bộ Môn', '25%').td                           ('Sửa', '10%').td('Xóa', '10%'), 'center');

	$ci=1;
	for ($i = $start; $i < $end; $i++)
	{
		// make sure that PHP doesn't try to show results that don't exist
		if ($i == $total_results) { break; }
		
		$id = mysql_result($result, $i, 'id');
		$id_unit = mysql_result($result, $i, 'maDonvi');
		$name_faculty = mysql_result($result, $i, 'tenKhoa');
		$name_subject = mysql_result($result, $i, 'tenBomon');
		
		$tmp = '<a href="Edit_Action_Donvi.php?id=' . $id . '"><img src="images/edit.png" alt="Show image error" /></a>';
		$temp = '<a href="Delete_Action_Donvi.php?id=' . $id . '"><img src="images/delete.png" alt="Show image error" /></a>';
		
		echo tr(td($ci).td($id_unit).td($name_faculty).td($name_subject).td($tmp).td($temp),'center');
		$ci++;
	}
	echo tbl_close();
	echo form_close();
	
	echo'<br />';
	
	//View button "Add"
	echo '<form action="Add_View_Donvi.php?action=ADD" method="POST">';
	echo '<center><input type="submit" name="submit" value="ADD" /></center>';
	echo '</form><br />';
	
	include('./index/footer.html');

?>
